package ma.controller.panels.parameterpanels;

import java.util.ArrayList;

import ma.controller.panels.auxillary.CtrInformationPanel;
import ma.controller.panels.auxillary.NumericalTFListener;
import ma.model.ManufacturingMethodInterface;
import ma.model.auxillary.Parameter;
import ma.view.gui.auxillary.NumericalTextField;

/**
 * The {@code CtrGeoPanel} controls and creates a sub
 * panel to hold geometry panels. A geometry panel consists of a parameter's
 * name, it's gui abbreviation, an input text field and it's unit. 
 * It registers adocument listener to every text field to check if valid
 * values were entered.
 * It controlls the number of geometry sets set by the user and dynamically
 * creates or deletes the geometry panels.
 * @author Oliver Eckstein
 *
 */
public abstract class CtrGeoPanel
    extends CtrInPanel {
    
    
    /**
     * To create a new instance of a
     * {@code GeometryParameterPanelFrameController}.
     * @param manuMethod the manufacturing method whose geometry gets set.
     */
    public CtrGeoPanel(
            ManufacturingMethodInterface manuMethod,
            CtrInformationPanel infoPanelCtr) {
        super(manuMethod, infoPanelCtr);
        createView();
        init();
        
        addTextFieldListenersAndRegisterObservers();
        manuMethod.registerObserver(panel);
    }
    
    @Override
    /*
     * (non-Javadoc)
     * @see ma.controller.gui.ParameterPanelController#addParameterPanels()
     */
    protected void init() {        
        for(int i = 0; i < manuMethod.getSetCount(); i++) {
            addGeometryParameterPanel(i);
        }                
    }
    
    /**
     * To add a new geometry set to the controller's view.
     * @param index the set's index.
     */
    public void addGeometryParameterPanel(int index) {
        //To retrieve all parameters (geometry & dependent) of a specific
        //index: d_11, d_21, l1, f_sv1, f_gamma1 etc.
        ArrayList<Parameter> paramList =
                manuMethod.getParametersByIndex(index);
        
        //Holds a single geometry set
        ArrayList<String[]> geometrySet =
                new ArrayList<String[]>(); 
                
        for(Parameter param : paramList) {
            if (param.getCategory() == Parameter.Type.GEOMETRY_PARAMETER) {
                
                String[] paramInformation = new String[5];
                paramInformation[0] = param.getName();
                paramInformation[1] = param.getAbbreviation();
                paramInformation[2] = param.getGuiAbbreviation();
                paramInformation[3] = param.getUnit(); 
                Double paramValue = param.getValue();
                if(paramValue == null) {
                    paramInformation[4] = "";
                } else if (paramValue.equals(Double.NaN)) {
                    paramInformation[4] = "";    
                } else {
                    paramInformation[4] = String.valueOf(paramValue);
                }   
                                    
                //Now a geometry set has been created
                geometrySet.add(paramInformation);
            }            
        }
        panel.addParameterPanel(geometrySet, manuMethod.getSetName() + " " + (index + 1));
    }
    
    @Override
    /*
     * (non-Javadoc)
     * @see ma.controller.panels.InputParameterPanelController#addTextFieldListenersAndRegisterObservers()
     */
    protected void addTextFieldListenersAndRegisterObservers() {
        Parameter param;
        String parameterAbbreviation;
        int i = 0;
        // Iterate through every text field set
        // then through every text field
        for(ArrayList <NumericalTextField> txtFields :
            panel.getGeometricValueTextFields().values()) {
            for(NumericalTextField txtField : txtFields) {            
                parameterAbbreviation = txtField.getIdentification();     
                param = manuMethod.
                            getParameter(parameterAbbreviation, i);
                //associate a text field with it's parameter
                txtField.addDocumentListener(new NumericalTFListener(
                                    txtField, manuMethod, param, this));           
                param.registerObserver(panel);                
            }
            i++;
        }        
    }
}
